;
; File:
;   $Source: /cvsroot/ijbswa/winsetup/privoxy_winthreads.nsi,v $
;
; Purpose:
;   NSIS script to make the Privoxy installer
;
;   This .NSI script is designed for NSIS v2.24+
;
;   Get NSIS from:  http://www.nullsoft.com/free/nsis/
;
; Copyright:
;   Written by and Copyright (C) 2007-2009 the Privoxy team.
;   http://www.privoxy.org/
;
;   This script originally written by and Copyright (C) 2002
;   Jonathan Foster
;
;   This program is free software; you can redistribute it 
;   and/or modify it under the terms of the GNU General
;   Public License as published by the Free Software
;   Foundation; either version 2 of the License, or (at
;   your option) any later version.
;
;   This program is distributed in the hope that it will
;   be useful, but WITHOUT ANY WARRANTY; without even the
;   implied warranty of MERCHANTABILITY or FITNESS FOR A
;   PARTICULAR PURPOSE.  See the GNU General Public
;   License for more details.
;
;   The GNU General Public License should be included with
;   this file.  If not, you can view it at
;   http://www.gnu.org/copyleft/gpl.html
;   or write to the Free Software Foundation, Inc., 59
;   Temple Place - Suite 330, Boston, MA  02111-1307, USA.
;

!include nsis\WinMessages.nsh ; to send close message to Privoxy process

Name "Privoxy"
OutFile "privoxy_setup.exe"
;Icon "build\privoxy.exe"

BGGradient off

; Some default compiler settings (uncomment and change at will):
SetCompress auto ; (can be off or force)
SetCompressor /FINAL /SOLID lzma
SetDatablockOptimize on
CRCCheck on
AutoCloseWindow true ; (can be true for the window go away automatically at end)
ShowInstDetails nevershow ; (can be show to have them shown, or nevershow to disable)
SetDateSave on ; (can be on to have files restored to their orginal date)
; SetOverwrite ifnewer ; (files are only overwritten if the existing file is older than the new file)
SetOverwrite on  ; install package files over-write existing files regardless of date

#LicenseText "You must agree to this license before installing."
#LicenseText "Privoxy is distributed under the GNU General Public License.  Please read it before you install."
#LicenseData "build/LICENSE.txt"

InstallDir "$PROGRAMFILES\Privoxy"
;InstallDirRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Privoxy.org\Privoxy" ""
;DirShow show ; (make this hide to not let the user change it)
;DirShow doesn't currently work.
DirText "Select the directory to install Privoxy in:"

ComponentText "Please select how you want to start Privoxy:"

Section "" ; (default section)
	; Close privoxy.exe if it is running (user is upgrading) to prevent in-use errors.
	FindWindow $R0 "PrivoxyLogOwner"
	SendMessage $R0 ${WM_CLOSE} 0 0

	; add files / whatever that need to be installed here.
	SetOutPath "$INSTDIR"

        ; save files the user might have changed
        ;   config.txt  match-all.action  trust.txt  user.action  user.filter
        ;   NOTE: rename fails if the destination file already exists
        ;
        IfFileExists "$INSTDIR\config.txt" 0 +3
        delete "$INSTDIR\old_config.txt"
        rename "$INSTDIR\config.txt"  "$INSTDIR\old_config.txt"
        ;
        IfFileExists "$INSTDIR\match-all.action" 0 +3
        delete "$INSTDIR\old_match-all.action"
        rename "$INSTDIR\match-all.action"  "$INSTDIR\old_match-all.action"
        ;
        IfFileExists "$INSTDIR\trust.txt" 0 +3
        delete "$INSTDIR\old_trust.txt"
        rename "$INSTDIR\trust.txt"  "$INSTDIR\old_trust.txt"
        ;
        IfFileExists "$INSTDIR\user.action" 0 +3
        delete "$INSTDIR\old_user.action"
        rename "$INSTDIR\user.action"  "$INSTDIR\old_user.action"
        ;
        IfFileExists "$INSTDIR\user.filter" 0 +3
        delete "$INSTDIR\old_user.filter"
        rename "$INSTDIR\user.filter"  "$INSTDIR\old_user.filter"
        ;
        IfFileExists "$INSTDIR\trust.txt" 0 +3
        delete "$INSTDIR\old_trust.txt"
        rename "$INSTDIR\trust.txt"  "$INSTDIR\old_trust.txt"

	File /r build\*.*

	;WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Privoxy.org\Privoxy" "" "$INSTDIR"
	WriteRegStr HKEY_LOCAL_MACHINE "Software\Microsoft\Windows\CurrentVersion\Uninstall\Privoxy" "DisplayName" "Privoxy (remove only)"
	WriteRegStr HKEY_LOCAL_MACHINE "Software\Microsoft\Windows\CurrentVersion\Uninstall\Privoxy" "UninstallString" '"$INSTDIR\privoxy_uninstall.exe"'

	WriteRegStr HKEY_CLASSES_ROOT "PrivoxyActionFile\shell\open\command" "" 'Notepad.exe "%1"'
	WriteRegStr HKEY_CLASSES_ROOT ".action" "" "PrivoxyActionFile"
	WriteRegStr HKEY_CLASSES_ROOT "PrivoxyFilterFile\shell\open\command" "" 'Notepad.exe "%1"'
	WriteRegStr HKEY_CLASSES_ROOT ".filter" "" "PrivoxyFilterFile"

	WriteUninstaller "privoxy_uninstall.exe"
SectionEnd

Section "Add to Start Menu"
	SetShellVarContext all ; (Add to "All Users" Start Menu if possible)
	RMDir /r "$SMPROGRAMS\Privoxy"
	CreateDirectory "$SMPROGRAMS\Privoxy"
	CreateShortCut "$SMPROGRAMS\Privoxy\Privoxy.lnk" "$INSTDIR\privoxy.exe"
	WriteINIStr "$SMPROGRAMS\Privoxy\Web-based Configuration.url" "InternetShortcut" "URL" "http://config.privoxy.org/"
	CreateShortCut "$SMPROGRAMS\Privoxy\Web-based Feedback.lnk" "$INSTDIR\doc\user-manual\contact.html"
	CreateDirectory "$SMPROGRAMS\Privoxy\Edit Config"
	CreateShortCut "$SMPROGRAMS\Privoxy\Edit Config\Main Configuration.lnk" "Notepad.exe" '"$INSTDIR\config.txt"'
	CreateShortCut "$SMPROGRAMS\Privoxy\Edit Config\Default Actions.lnk" "Notepad.exe" '"$INSTDIR\default.action"'
	CreateShortCut "$SMPROGRAMS\Privoxy\Edit Config\User Actions.lnk" "Notepad.exe" '"$INSTDIR\user.action"'
	CreateShortCut "$SMPROGRAMS\Privoxy\Edit Config\Filters.lnk" "Notepad.exe" '"$INSTDIR\default.filter"'
	CreateShortCut "$SMPROGRAMS\Privoxy\Edit Config\Trust list.lnk" "Notepad.exe" '"$INSTDIR\trust.txt"'
	CreateDirectory "$SMPROGRAMS\Privoxy\Documentation"
	CreateShortCut "$SMPROGRAMS\Privoxy\Documentation\User Manual.lnk" "$INSTDIR\doc\user-manual\index.html"
	CreateShortCut "$SMPROGRAMS\Privoxy\Documentation\Frequently Asked Questions.lnk" "$INSTDIR\doc\faq\index.html"
	CreateShortCut "$SMPROGRAMS\Privoxy\Documentation\Credits.lnk" "Notepad.exe" '"$INSTDIR\AUTHORS.txt"'
	CreateShortCut "$SMPROGRAMS\Privoxy\Documentation\License.lnk" "Notepad.exe" '"$INSTDIR\LICENSE.txt"'
	CreateShortCut "$SMPROGRAMS\Privoxy\Documentation\ReadMe file.lnk" "Notepad.exe" '"$INSTDIR\README.txt"'
	WriteINIStr "$SMPROGRAMS\Privoxy\Documentation\Web Site.url" "InternetShortcut" "URL" "http://privoxy.org/"
	CreateShortCut "$SMPROGRAMS\Privoxy\Uninstall Privoxy.lnk" "$INSTDIR\privoxy_uninstall.exe"
SectionEnd


Section "Run automatically at startup"
	CreateShortCut "$SMSTARTUP\Privoxy.lnk" "$INSTDIR\privoxy.exe" "" "" 0 SW_SHOWMINIMIZED
SectionEnd

Section "" ; (default section)
	MessageBox MB_OK "Privoxy has now been installed"
SectionEnd

Section "Start Privoxy now"
	SetOutPath "$INSTDIR"
	Exec "$INSTDIR\privoxy.exe"
SectionEnd

; begin uninstall settings/section. The UninstallText line must be before the Section header.
UninstallText "This will uninstall Privoxy from your system"
Section Uninstall
	SetShellVarContext all ; (Remove from "All Users" Start Menu if possible)
	
	;DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Privoxy.org\Privoxy"
	DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Privoxy"

	DeleteRegKey HKEY_CLASSES_ROOT ".action"
	DeleteRegKey HKEY_CLASSES_ROOT "PrivoxyActionFile"
	DeleteRegKey HKEY_CLASSES_ROOT ".filter"
	DeleteRegKey HKEY_CLASSES_ROOT "PrivoxyFilterFile"

	Delete "$SMSTARTUP\Privoxy.lnk"

	;Close privoxy.exe
	FindWindow $R0 "PrivoxyLogOwner"
	SendMessage $R0 ${WM_CLOSE} 0 0

	RMDir /r "$SMPROGRAMS\Privoxy"
	RMDir /r "$INSTDIR"
SectionEnd

; eof
